package com.ruochen.recipe.ui.fragment.personal;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.SizeUtils;
import com.lxj.xpopup.XPopup;
import com.ruochen.common.adapter.AbsCommonAdapter;
import com.ruochen.common.adapter.AbsViewHolder;
import com.ruochen.common.adapter.RecyclerCommonAdapter;
import com.ruochen.common.adapter.RecyclerViewHolder;
import com.ruochen.common.base.BaseMvpFragment;
import com.ruochen.common.widget.NonSlidingGridView;
import com.ruochen.recipe.R;
import com.ruochen.recipe.customer.CookDishEditPopup;
import com.ruochen.recipe.databinding.FragmentBindCookbookBinding;
import com.ruochen.recipe.entity.CookDishEntity;
import com.ruochen.recipe.entity.CookbookEntity;
import com.ruochen.recipe.mvp.personal.BindCookbookCovenant;
import com.ruochen.recipe.mvp.personal.BindCookbookPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 绑定菜谱
 * @Auth RuoChen
 * @Date 14/03/2021, 14:41
 **/
public class BindCookbookFragment extends BaseMvpFragment<FragmentBindCookbookBinding, BindCookbookPresenter> implements BindCookbookCovenant.MvpView {

    @Override
    protected void initView(Bundle savedInstanceState) {
        List<CookbookEntity> list = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            final List<CookDishEntity> childList = new ArrayList<>(i);
            for (int j = 0; j < i; j++) {
                childList.add(new CookDishEntity("菜肴 " + j));
            }
            list.add(new CookbookEntity("菜谱 " + i, childList));
        }

        viewBinding.nsrvMenuExpandable.setLayoutManager(new LinearLayoutManager(getContext()));
        viewBinding.nsrvMenuExpandable.setAdapter(new RecyclerCommonAdapter<CookbookEntity>(getContext(), R.layout.item_cookbook_group, list) {
            @Override
            protected void convert(RecyclerViewHolder holder, CookbookEntity item, int position) {
                holder.setText(R.id.rtvItemName, item.getName());
                NonSlidingGridView nsgvItemDishGrid = holder.getView(R.id.nsgvItemDishGrid);
                if (item.getChildList() != null && item.getChildList().size() > 0) {
                    nsgvItemDishGrid.setVisibility(View.VISIBLE);
                    nsgvItemDishGrid.setAdapter(new AbsCommonAdapter<CookDishEntity>(getContext(), R.layout.item_cookbook_child, item.getChildList()) {
                        @Override
                        public void convert(AbsViewHolder childHolder, CookDishEntity item, int position) {

                        }
                    });
                    nsgvItemDishGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            showChooseDish();
                        }
                    });
                } else {
                    nsgvItemDishGrid.setVisibility(View.GONE);
                }
            }
        });
    }

    private void showChooseDish() {
        new XPopup.Builder(getContext())
                .dismissOnTouchOutside(false)
                .dismissOnTouchOutside(false)
                .enableDrag(false)
                .autoOpenSoftInput(false)
                .autoFocusEditText(false)
                .moveUpToKeyboard(false)
                .popupWidth(SizeUtils.dp2px(540))
                .isDestroyOnDismiss(true)
                .asCustom(new CookDishEditPopup(getContext()))
                .show();
    }

    @Override
    protected BindCookbookPresenter createPresenter() {
        return new BindCookbookPresenter(this);
    }

}
